package com.madou.dbtool.mysql.test;

import com.alibaba.fastjson.JSON;
import com.mysql.cj.xdevapi.JsonLiteral;
import com.mysql.cj.xdevapi.JsonString;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.StringJoiner;

/**
 * 收集mysql文档信息
 * @author miukoo
 */
public class GenMysqlDoc {
    // http://www.deituicms.com/mysql8cn/cn/web.html
    public static void main(String[] args) throws IOException {
        Map<String,Map<String,String>> data = new HashMap();
        Document document = Jsoup.parse(new File("C:\\Users\\miukoo\\Desktop\\keyword.html"), "utf-8");
        Elements select = document.select(".literal");
        StringJoiner joiner = new StringJoiner(";");
        for (Element element : select) {
            String name = element.text().toLowerCase(Locale.ROOT);
            if(name.length()<3){
                continue;
            }
           data.put(name,new HashMap<>());
        }
        data.put("in",new HashMap<>());
        data.put("or",new HashMap<>());
        data.put("as",new HashMap<>());
        data.put("at",new HashMap<>());
        data.put("pi",new HashMap<>());
        data.put("if",new HashMap<>());
        data.put("is",new HashMap<>());
        data.put("no",new HashMap<>());
        data.put("of",new HashMap<>());
        data.put("to",new HashMap<>());

        Document doc = Jsoup.parse(new File("C:\\Users\\miukoo\\Desktop\\web-main.html"), "utf-8");
        Elements elements = doc.select(".table-contents table").eq(0).select("tr");
        for (int i = 1; i < elements.size(); i++) {
            Element element = elements.get(i);
            try {
                String name = element.select("td").first().text().toLowerCase(Locale.ROOT);
                boolean addKuoHao = false;
                if(name.contains("()")){
                    addKuoHao = true;
                    name = name.replace("()", "");
                }
                String desc = element.select("td").last().text();
                String tagName = ".listitem a[name=function_" + name.replace("_", "-") + "]";
                select = doc.select(tagName);
                String code = "";
                if (select != null && select.size() > 0) {
                    Element parent = select.get(0).parent();
                    parent.select("a").remove();
                    parent.select("font").removeAttr("style").tagName("span");
                    code = parent.html();
                }
                if (data.containsKey(name)) {
                    data.remove(name);
                }
                if(addKuoHao) {
                    name += "()";
                }
                Map<String, String> map = data.get(name);
                if (map == null) {
                    map = new HashMap<>();
                }
                if(name.length()>50){
                    continue;
                }
                map.put("desc", desc);
                map.put("code", code);
                data.put(name, map);
            }catch (Exception e){
                System.out.println(element);
                e.printStackTrace();
            }
//            if(i>5){
//                break;
//            }
        }
        String str = JSON.toJSONString(data);
        Files.write(Paths.get("C:\\Users\\miukoo\\Desktop\\1.txt"),str.getBytes());
    }
}
